// Exception Handling support header for -*- C++ -*-
// Copyright (C) 1995, 96-97, 1998 Free Software Foundation
// Author: Landfox

#ifndef _CLASS_EXCEPTION__
#define _CLASS_EXCEPTION__

#include <exception>
//#include "C:\\Documents and Settings\\sfaktorovich\\My Documents\\remoteconsole2\\vld.h"

namespace std
{
#ifndef _WIN32

	class cException : public exception
	{
	private:
		struct str_mem 
		{
			long count;
			char * buffer;
		};
		struct str_mem * memory;

	public:
		cException()
		{
			memory = 0;
		}

		explicit cException(const char * msg)
		{
			memory = new struct str_mem;
			memory->count = 1;
			memory->buffer = new char[strlen(msg) + 1];
			strcpy(memory->buffer, msg);
		}

		cException(const cException& e)
		{
			memory = e.memory;
			if(memory)
				memory->count++;
		}

	  	virtual ~cException() throw()
		{
			if(memory)
			{
				if(memory->count == 1)
				{
					delete[] memory->buffer;
					delete memory;
				}
				else
				{
					memory->count--;
				}
			}
		}

		cException& operator=(const cException& e)
		{
			if(this != &e)
			{
				if(memory && !(--(memory->count)))
				{
					delete[] memory->buffer;
					delete memory;
				}
				memory = e.memory;
				if(e.memory)
					(memory->count)++;
			}
			return *this;
		}
		bool operator==(const cException& e)
		{
			if(this != &e)
			{
				return false;
			}
			return true;
		}

		virtual const char* what() const throw()
		{
			return memory?(memory->buffer):0;
		}
	};

#else

typedef exception cException;

#endif
} // end of namespace std
#endif
